package com.shaoyu.elasticsearch.util;
import ch.qos.logback.classic.pattern.ClassicConverter;
import ch.qos.logback.classic.spi.ILoggingEvent;

import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * @Description: 获取容器IP地址 添加到logback日志中
 * @Author: shaoyu1
 * @Date Create in 2018/2/2 11:33
 */
public class IpConvert extends ClassicConverter {
    private static String hostIp;
    @Override
    public String convert(ILoggingEvent event) {
        if (hostIp == null) {
            InetAddress address = null;
            try {
                address = InetAddress.getLocalHost();
            }
            catch (UnknownHostException e) {
                e.printStackTrace();
            }
            hostIp = address.getHostAddress();
        }
        return hostIp;
    }
}